package ac.kr.ml.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ac.kr.ml.vo.ReplyVo;
import ac.kr.ml.vo.WriteVo;

public class ReplyDao {
	
String LOCALHOST = "partner.medialog.co.kr";
	
	public List<ReplyVo> getList(int con_id){

		List<ReplyVo> list = new ArrayList<ReplyVo>();
		
		Connection conn = null;
		Statement stmt= null;

		try {
			Class.forName("com.mysql.jdbc.Driver");

			String dburl = "jdbc:mysql://"+LOCALHOST+":3306/freshman";
			conn = DriverManager.getConnection(dburl, "freshman", "freshman!$");


			stmt = conn.createStatement();

			String sql = "select * from REPLY where content_id='"+con_id+"' order by re_id asc";

			ResultSet rs = stmt.executeQuery(sql);
			System.out.println("rs : "+rs);
			while(rs.next()){
				ReplyVo  vo = new ReplyVo();
				vo.setRe_id(rs.getInt(1));
				vo.setCountent_id(rs.getInt(2));
				vo.setCommend(rs.getString(3));
				vo.setRe_writer(rs.getInt(4));
				list.add(vo);
			}

			rs.close();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException ex){
			ex.printStackTrace();
		} finally{
			try{
				stmt.close();
			}catch(SQLException e){}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException e){}
			}

		}
		return list;
	}
	
	
	public int insert(ReplyVo vo){

		int re_id =0;
		Connection conn = null;		
		PreparedStatement pstmt= null;
		try {
			Class.forName("com.mysql.jdbc.Driver");

			String dburl = "jdbc:mysql://"+LOCALHOST+":3306/freshman";
			conn = DriverManager.getConnection(dburl, "freshman", "freshman!$");

			String sql = "insert into REPLY (content_id, commend, re_writer) values(?,?,?)";

			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, vo.getCountent_id());
			pstmt.setString(2, vo.getCommend());
			pstmt.setInt(3, vo.getRe_writer());
			
			pstmt.execute();			
			
			sql = "select LAST_INSERT_ID()";
			pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				re_id = rs.getInt(1);
			}

			rs.close();


		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException ex){
			ex.printStackTrace();
		} finally{
			try{
				
				pstmt.close();

			}catch(SQLException e){}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException e){}
			}

		}
		return re_id;
	}
	

	public ReplyVo getReply(){
		
		
		return null;
	}
	
}
